/*
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 * This file is part of the lwIP TCP/IP stack.
 *
 * Author: Huawei Technologies
 *
 */

#ifndef __LWIPGZ_OFFLOAD_H__
#define __LWIPGZ_OFFLOAD_H__

#include "lwipopts.h"

#if GAZELLE_ENABLE
#include <stdbool.h>
#include <rte_ethdev.h>
#include "dpdk_version.h"

#if CHECKSUM_OFFLOAD_ALL
#include <rte_mbuf_core.h>
#include "lwip/pbuf.h"
#endif

#define PBUF_TO_MBUF(p)  ((struct rte_mbuf *)RTE_PTR_SUB(p, sizeof(struct rte_mbuf)))

static inline void pbuf_offload_copy(struct pbuf *to, const struct pbuf *from)
{
    PBUF_TO_MBUF(to)->l4_len   = PBUF_TO_MBUF(from)->l4_len;
    PBUF_TO_MBUF(to)->l3_len   = PBUF_TO_MBUF(from)->l3_len;
    PBUF_TO_MBUF(to)->l2_len   = PBUF_TO_MBUF(from)->l2_len;
    PBUF_TO_MBUF(to)->ol_flags = PBUF_TO_MBUF(from)->ol_flags;
}

static inline void pbuf_set_vlan(struct pbuf *p, u16_t vlan_tci)
{
    PBUF_TO_MBUF(p)->ol_flags |= RTE_MBUF_F_TX_VLAN;
    PBUF_TO_MBUF(p)->vlan_tci = vlan_tci;
}

#if CHECKSUM_CHECK_IP_HW
// for ip4_input
static inline u64_t is_cksum_ipbad(struct pbuf *p) {
    return PBUF_TO_MBUF(p)->ol_flags & (RTE_MBUF_F_RX_IP_CKSUM_BAD);
}
#endif /* CHECKSUM_CHECK_IP_HW */

#if (CHECKSUM_CHECK_TCP_HW || CHECKSUM_CHECK_UDP_HW)
// for tcp_input and udp_input
static inline u64_t is_cksum_bad(struct pbuf *p) {
    return PBUF_TO_MBUF(p)->ol_flags & (RTE_MBUF_F_RX_L4_CKSUM_BAD);
}
#endif /* (CHECKSUM_CHECK_TCP_HW || CHECKSUM_CHECK_UDP_HW) */

#if CHECKSUM_GEN_IP_HW
static inline void ethh_cksum_set(struct pbuf *p, u16_t len) {
    PBUF_TO_MBUF(p)->l2_len = len;
}

// replaces IPH_CHKSUM_SET
static inline void iph_cksum_set(struct pbuf *p, u16_t len, bool do_ipcksum) {
    PBUF_TO_MBUF(p)->ol_flags |= ((len == IP_HLEN) ? RTE_MBUF_F_TX_IPV4 : RTE_MBUF_F_TX_IPV6);
    if (do_ipcksum) {
        PBUF_TO_MBUF(p)->ol_flags |= RTE_MBUF_F_TX_IP_CKSUM;
    }
    PBUF_TO_MBUF(p)->l3_len = len;
}
#endif /* CHECKSUM_GEN_IP_HW */

// replace ip_chksum_pseudo
#if (CHECKSUM_GEN_TCP_HW || CHECKSUM_GEN_UDP_HW)
#include <rte_ip.h>

#if CHECKSUM_GEN_TCP_HW
static inline void tcph_cksum_set(struct pbuf *p, u16_t len) {
    PBUF_TO_MBUF(p)->l4_len = len;
    PBUF_TO_MBUF(p)->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
}
#endif /* CHECKSUM_GEN_TCP_HW */

#if CHECKSUM_GEN_UDP_HW
static inline void udph_cksum_set(struct pbuf *p, u16_t len) {
    PBUF_TO_MBUF(p)->l4_len = len;
    PBUF_TO_MBUF(p)->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
}
#endif /* CHECKSUM_GEN_UDP_HW */
#endif /* (CHECKSUM_GEN_TCP_HW || CHECKSUM_GEN_UDP_HW) */

#endif /* GAZELLE_ENABLE */
#endif /* __LWIPGZ_OFFLOAD_H__ */
